﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WX.WMS.DataModel;
using WX.WMS.Common;


namespace WX.WMS.BLL
{
    public class InStockBLL : BaseBLL
    {
        private BaseDAO DAO = null;
        public InStockBLL()
        {
            DAO = new BaseDAO();
        }

        public bool IsExisted(string SerialNo, string InStockID)
        {
            if (DAO.T_INSTOCKs.Where(t => t.SERIAL_NO == Convert.ToInt32(SerialNo) 
                                        && t.INSTOCK_ID.ToLower() == InStockID.ToLower()).Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IEnumerable<T_INSTOCK> GetInStockList()
        {
            var list = DAO.T_INSTOCKs.Where(WhereCondition);

            if (OrderCondition != "")
            {
                list = list.OrderBy(OrderCondition);
            }
            else {
                list = list.OrderByDescending(t => t.INSTOCK_DATE);
            }

            return list;
        }

        public T_INSTOCK GetInStockByID(string ID)
        {
            return DAO.T_INSTOCKs.Where(t => t.ID.ToString() == ID).SingleOrDefault();
        }
        public IEnumerable<T_INSTOCK_ITEM> GetInStockDetailByStockID(string ID)
        {
            var list = DAO.T_INSTOCK_ITEMs.Where(t => t.INSTOCK_ID.ToString() == ID);
            if (!string.IsNullOrEmpty(WhereCondition))
            {
                list = list.Where(WhereCondition);
            }
            if (!string.IsNullOrEmpty(OrderCondition.Trim()))
            {
                list = list.OrderBy(OrderCondition);
            }
            return list;
        }
        public int AddInStock(T_INSTOCK InStock)
        {
            try
            {
                InStock.ID = DAO.GetGUID();

                DAO.T_INSTOCKs.InsertOnSubmit(InStock);
                DAO.SubmitChanges();

                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AddInStock(T_INSTOCK InStock, out string InStockGuid)
        {
            try
            {
                InStock.ID = DAO.GetGUID();
                
                DAO.T_INSTOCKs.InsertOnSubmit(InStock);
                DAO.SubmitChanges();

                InStockGuid = InStock.ID.ToString();
                return 1;
            }
            catch (Exception ex)
            {
                InStockGuid = "";
                return -1;
            }
        }

        public int UpdateInStock(T_INSTOCK InStock)
        {
            try
            {
                T_INSTOCK updateEntity = DAO.T_INSTOCKs.SingleOrDefault(t=>t.ID.ToString()==InStock.ID.ToString());
                if (updateEntity == null) 
                    return -1;

                updateEntity.APPLY_ID = InStock.APPLY_ID;
                updateEntity.AUDIT_TIME = InStock.AUDIT_TIME;
                updateEntity.AUDITOR_ID = InStock.AUDITOR_ID;
                updateEntity.CLIENT_ID = InStock.CLIENT_ID;
                updateEntity.CREATE_TIME = InStock.CREATE_TIME;
                updateEntity.CREATOR_ID = InStock.CREATOR_ID;
                updateEntity.E_ACCOUNT = InStock.E_ACCOUNT;
                updateEntity.INSTOCK_DATE = InStock.INSTOCK_DATE;
                updateEntity.INSTOCK_ID = InStock.INSTOCK_ID;
                //updateEntity.SERIAL_NO = InStock.SERIAL_NO;
                updateEntity.STATUS = InStock.STATUS;
                updateEntity.TYPE = InStock.TYPE;
                updateEntity.Remark = InStock.Remark;
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteInStock(string ID)
        {
            try
            {
                T_INSTOCK deleteEntity = DAO.T_INSTOCKs.SingleOrDefault(t => t.ID.ToString() == ID);
                if (deleteEntity == null) 
                    return -1;
                DAO.T_INSTOCK_ITEMs.DeleteAllOnSubmit(deleteEntity.T_INSTOCK_ITEMs);
                DAO.T_INSTOCKs.DeleteOnSubmit(deleteEntity);
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int DeleteInStocks(List<string> IDs)
        {
            try
            {
                foreach (string ID in IDs)
                {
                    T_INSTOCK deleteEntity = DAO.T_INSTOCKs.SingleOrDefault(t => t.ID.ToString() == ID);
                    if (deleteEntity == null)
                        return -1;
                    DAO.T_INSTOCK_ITEMs.DeleteAllOnSubmit(deleteEntity.T_INSTOCK_ITEMs);
                    DAO.T_INSTOCKs.DeleteOnSubmit(deleteEntity);
                }
                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int AuditInStock(string ID, Guid UserID)
        {
            try
            {
                DAO.BeginTransaction();
                T_INSTOCK entity = DAO.T_INSTOCKs.SingleOrDefault(t => t.ID.ToString() == ID);

                entity.STATUS = (int)Enumerations.StockStatus.审核通过;
                entity.AUDIT_TIME = DateTime.Now;
                entity.AUDITOR_ID = UserID;

                foreach (T_INSTOCK_ITEM item in entity.T_INSTOCK_ITEMs)
                {
                    if (DAO.T_STOREs.Where(t => t.PRODUCT_DETAIL_ID.ToString() == item.PRODUCT_DETAIL_ID.ToString()
                                            && t.FloorID.ToString() == item.FLOOR_ID.ToString()).Count() > 0)
                    {
                        T_STORE store = DAO.T_STOREs.SingleOrDefault(t => t.PRODUCT_DETAIL_ID.ToString() == item.PRODUCT_DETAIL_ID.ToString()
                                            && t.FloorID.ToString() == item.FLOOR_ID.ToString());
                        
                        store.GROSS_WEIGHT += (double)item.GROSS_WEIGHT;
                        store.NET_WEIGHT += (double)item.NET_WEIGHT;
                        store.PACKET_QTY += (double)item.PACKET_QTY;
                        store.PACKET_QTY2 += (double)item.PACKET_QTY2;
                        
                    }
                    else
                    {
                        T_STORE store = new T_STORE();
                        store.ID = DAO.GetGUID();
                        store.FloorID = item.FLOOR_ID;
                        store.PRODUCT_DETAIL_ID = (Guid)item.PRODUCT_DETAIL_ID;
                        store.PRODUCT_ID = item.T_PRODUCT_DETAIL.PRODUCT_ID;
                        store.GROSS_WEIGHT = (double)item.GROSS_WEIGHT;
                        store.NET_WEIGHT = (double)item.NET_WEIGHT;
                        store.PACKET_QTY = (double)item.PACKET_QTY;
                        store.PACKET_QTY2 = (double)item.PACKET_QTY2;
                        DAO.T_STOREs.InsertOnSubmit(store);

                    }

                    DAO.SubmitChanges();
                }
                DAO.SubmitChanges();
                DAO.Commit();
                return 1;
            }
            catch (Exception ex)
            {
                DAO.RollBack();
                return -1;
            }
        }

        public int BackInStock(string ID)
        {
            try
            {
                T_INSTOCK entity = DAO.T_INSTOCKs.SingleOrDefault(t => t.ID.ToString() == ID);

                entity.STATUS = (int)Enumerations.StockStatus.打回;

                DAO.SubmitChanges();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public bool IsAudited(string ID)
        {
            T_INSTOCK entity = DAO.T_INSTOCKs.SingleOrDefault(t => t.ID.ToString() == ID);

            return entity.STATUS == (int)Enumerations.StockStatus.审核通过 ? true : false;
        }
    }
}
